package com.zx.map.controller;

import com.zx.map.service.CustAcctLogService;
import com.zx.map.service.domain.CustAcctLogDownloadDomain;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.CustAcctLogModel;
import com.zx.map.utils.SUConstants;
import com.zx.mobile.domain.CustAcctLogQueryDomain;
import com.zx.mobile.domain.CustAcctLogSummary;
import com.zx.platform.utils.PmpUtils;
import com.zx.platform.utils.report.ReportUtil;
import com.zx.utils.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * 交易流水
 */
@Controller
@RequestMapping("/manage/custAcctLog")
public class CustAcctLogController {
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(CustAcctLogController.class);

	@Autowired
	private CustAcctLogService custAcctLogService;


	/**
	 * 账务流水查询功能 *
	 *
	 * @param custAccLogQueryDomain
	 * @param model
	 * @param request
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/queryAcctLogList")
	public String queryAcctLogList(CustAcctLogQueryDomain custAccLogQueryDomain,
	                               Model model, HttpServletRequest request) {
		try {// 当前登录用户对应的代理商
			AgentInfoModel curAgentInfoModel = WebUtils.queryCurrentLoginAgentInfo(request);

			Integer pageNum = custAccLogQueryDomain.getPageNum();
			Integer pageSize = custAccLogQueryDomain.getPageSize();
			if (null == pageNum) {
				pageNum = 1;
				custAccLogQueryDomain.setPageNum(1);
			}
			if (null == pageSize) {
				pageSize = SUConstants.PAGE_COUNT;
				custAccLogQueryDomain.setPageSize(SUConstants.PAGE_COUNT);
			}
			if (StringUtils.isBlank(custAccLogQueryDomain.getBeginDate())) {
				custAccLogQueryDomain.setBeginDate(PmpUtils.dateToString(
						new Date(), "yyyy-MM-dd"));
			}
			if (StringUtils.isBlank(custAccLogQueryDomain.getEndDate())) {
				custAccLogQueryDomain.setEndDate(PmpUtils.dateToString(
						new Date(), "yyyy-MM-dd"));
			}

			Map map = custAcctLogService.queryAcctLogReportPageByDomain(custAccLogQueryDomain, curAgentInfoModel);
			int recordCount=Integer.parseInt(map.get("count").toString());
			List<CustAcctLogDownloadDomain> reportList= (List<CustAcctLogDownloadDomain>) map.get("list");

			if (pageSize * pageNum > recordCount + pageSize - 1) {
				pageNum = 1;
			}
			model.addAttribute("list", reportList);
			model.addAttribute("pageNum", pageNum);
			model.addAttribute("pageSize", pageSize);
			model.addAttribute("pageCount",
					PmpUtils.getPageCount(recordCount, pageSize));
			model.addAttribute("recordCount", recordCount);
			model.addAttribute("model", custAccLogQueryDomain);
			return "/jsp/map/custAcctLog/custAcctLogList";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
	 * 账户流水下载
	 *
	 * @param custAccLogQueryDomain
	 * @param model
	 * @param request
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/downCustAcctList")
	public String downCustAcctList(
			CustAcctLogQueryDomain custAccLogQueryDomain, Model model,
			HttpServletRequest request, HttpServletResponse response) {

		try {
			AgentInfoModel curAgentInfoModel = WebUtils.queryCurrentLoginAgentInfo(request);
			if (StringUtils.isBlank(custAccLogQueryDomain.getBeginDate())) {
				custAccLogQueryDomain.setBeginDate(PmpUtils.dateToString(
						new Date(), "yyyy-MM-dd"));
			}
			if (StringUtils.isBlank(custAccLogQueryDomain.getEndDate())) {
				custAccLogQueryDomain.setEndDate(PmpUtils.dateToString(
						new Date(), "yyyy-MM-dd"));
			}
			List<CustAcctLogDownloadDomain> reportList = custAcctLogService.queryAcctLogReportList(custAccLogQueryDomain,curAgentInfoModel);

			Map<Integer, Object> formatAttr = new HashMap<Integer, Object>();
			formatAttr.put(1, ReportUtil.Date_cro);
			formatAttr.put(6, ReportUtil.Decimal_Money_chn);
			formatAttr.put(7, ReportUtil.Decimal_Money_chn);
			try {

				String[] header = null;
				header = new String[]{"创建日期", "账户流水", "账号", "客户名称", "交易类型",
						"交易金额", "账户余额", "订单号", "备注"};
				String sts = ReportUtil.exportReport(header, reportList, null,
						"账务流水查询", formatAttr, null, response);
				String[] st = sts.split("#");
				ReportUtil.download(response, st[0], st[1]);
			} catch (Exception e) {
				log.error("账务流水", e);
			}
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
		return null;
	}


	/**
	 * 账务流水汇总功能
	 *
	 * @param custAccLogQueryDomain
	 * @param model
	 * @param request
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/custAcctLogSummary")
	public String custAcctLogSummary(CustAcctLogQueryDomain custAccLogQueryDomain, Model model, HttpServletRequest request) {
		try {
			if (StringUtils.isBlank(custAccLogQueryDomain.getBeginDate())) {
				custAccLogQueryDomain.setBeginDate(PmpUtils.dateToString(
						new Date(), "yyyy-MM-dd"));
			}
			if (StringUtils.isBlank(custAccLogQueryDomain.getEndDate())) {
				custAccLogQueryDomain.setEndDate(PmpUtils.dateToString(
						new Date(), "yyyy-MM-dd"));
			}

			AgentInfoModel curAgentInfoModel = WebUtils.queryCurrentLoginAgentInfo(request);
			List<CustAcctLogSummary> custAcctLogList = custAcctLogService.queryAcctLogSummaryList(custAccLogQueryDomain, curAgentInfoModel);

			model.addAttribute("custMap", custAcctLogList);
			model.addAttribute("model", custAccLogQueryDomain);
			return "/jsp/map/custAcctLog/custAcctLogSummary";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	@RequestMapping("/downAcctLogSummary")
	public String downAcctLogSummary(CustAcctLogQueryDomain custAccLogQueryDomain,Model model, HttpServletRequest request,HttpServletResponse response) {
		try {
			if (StringUtils.isBlank(custAccLogQueryDomain.getBeginDate())) {
				custAccLogQueryDomain.setBeginDate(PmpUtils.dateToString(
						new Date(), "yyyy-MM-dd"));
			}
			if (StringUtils.isBlank(custAccLogQueryDomain.getEndDate())) {
				custAccLogQueryDomain.setEndDate(PmpUtils.dateToString(
						new Date(), "yyyy-MM-dd"));
			}

			AgentInfoModel curAgentInfoModel = WebUtils.queryCurrentLoginAgentInfo(request);

			List<CustAcctLogSummary> acctLogSummaryList=custAcctLogService.queryAcctLogSummaryList(custAccLogQueryDomain,curAgentInfoModel);

			Map<Integer, Object> formatAttr = new HashMap<Integer, Object>();
			for (int i=3;i<=CustAcctLogSummary.class.getDeclaredFields().length;i++){
				formatAttr.put(i, ReportUtil.Decimal_Money_chn);
			}
			try {
				String[] header = new String[]{"账户","名称","期初账户","扫码充值(+)","在线充值(+)","刷卡充值(+)","T0取现(-)","T1取现(-)","	信用卡还款(-)	","游戏消费(-)","分享收益转出(+)","理论期末账户","实际期末账户"};
				String sts = ReportUtil.exportReport(header, acctLogSummaryList, null,
						"账务流水汇总", formatAttr, null, response);
				String[] st = sts.split("#");
				ReportUtil.download(response, st[0], st[1]);
			} catch (Exception e) {
				log.error("账务流水", e);
			}

		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
		return null;
	}

}
